package com.lagou;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Map;

public class MyThread extends Thread {

    private Socket socket;

    private Map<String, HttpServlet> map;

    public MyThread(Socket socket, Map<String, HttpServlet> map) {
        this.socket = socket;
        this.map = map;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();

            //封装request和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(outputStream);

            HttpServlet httpServlet = map.get(request.getUrl());
            if (httpServlet == null) {
                //静态资源
                response.outputHtml(request.getUrl());
            } else {
                httpServlet.service(request, response);
            }
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
